﻿#define _CRT_SECURE_NO_WARNINGS 1

// 游戏函数中的具体实现

#include"game.h"

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) {
	int i = 0;

	for (i = 0; i < rows; i++) {
		int j = 0;
		for (j = 0; j < cols; j++) {
			board[i][j] = set;
		}
	}
}

void DisplayBoard(char board[ROWS][COLS], int row, int col) {
	
	int i = 0;
	printf("-------扫雷游戏------ - \n");
	for (i = 0; i <= row; i++) {
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++) {
		printf("%d ", i);
		int j = 0;
		for (j = 1; j <= col; j++) {
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}

}

void SetMine(char board[ROWS][COLS], int row, int col) {

	// 布置10个雷
	int count = EASY_COUNT;
	// 生成随机坐标
	while (count) {
		int x = rand() % row + 1;
		int y = rand() % col + 1;

		if (board[x][y] == '0') {
			board[x][y] = '1';
			count--;
		}
	}
}

// 统计周围雷的数量
static int GetMineCount(char mine[ROWS][COLS], int x, int y) {
	// 每一个其实是一个acll码为48的一个数字
	return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] +
		mine[x][y - 1] + mine[x][y + 1] +
		mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1]-8*'0';
}


void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
	int x = 0;
	int y = 0;
	int win = 0;
	while (win<row*col-EASY_COUNT) {
		printf("请输出需要排查的坐标：>");
		scanf("%d%d", &x, &y);
		// 判断坐标是否合法 
		if (x >= 1 && x <= row && y >= 1 && y <= col) {
			
			// 判断是不是雷
			if (mine[x][y] == '1') {
				printf("很遗憾，你被炸死了\n");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else {
				// 不是雷，统计坐标周围的雷的数量
				int count = GetMineCount(mine,x,y);
				show[x][y] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}

		}
		else {
			printf("坐标⾮法，重新输⼊\n");
		}


		if (win == row * col - EASY_COUNT) {
			printf("恭喜你，排雷成功！\n");
			DisplayBoard(mine, ROW, COL);
		}

	}
}